<?php

/**
 * @see SmartL_Zend_Controller_Action
 */
require_once 'SmartL/Zend/Controller/Action.php';

require_once 'Db/Record/Status.php';
require_once 'Db/Record/User.php';
require_once 'Doctrine/Query.php';

require_once 'Zend/Paginator.php';
require_once 'SmartL/Zend/Paginator/Adapter/Doctrine.php';

class IndexController extends SmartL_Zend_Controller_Action 
{	
	public function userAction()
	{
		if ( Zend_Auth::getInstance()->hasIdentity() ) {
			$this->_statusForm();						
		}
		
		if ( $this->_hasParam('user') ) {
			$query = new Doctrine_Query();
			$user = $query->from('Db_Record_User')
				->where('username = ?', $this->_getParam('user'))
				->fetchOne();
				
			$this->view->user = $user; 
			$this->_statuses($user->id);		
		}
		else {
			$this->_helper->redirector->setGotoRoute(array('action'=>'index'), 'default', false);
		}
	}
	
	public function logoutAction()
	{
    	if ( Zend_Auth::getInstance()->hasIdentity() ) {
    		Zend_Auth::getInstance()->clearIdentity();		
    	}
		$this->_helper->redirector->setGotoRoute(array('action'=>'index'), 'default', false);
	}

	public function indexAction()
	{		
		$userId = $this->application->getLoginInstance()->getUserId();
		if ( $userId ) {			
			$this->_statuses( $userId );
			$this->_statusForm();
			$this->view->getHelper ("translate")->setTranslator ($this->translations['userTranslations']);
			$this->render('user');
		}
		else {
			$query = new Doctrine_Query();
			$users = $query->from('Db_Record_User u')->fetchArray();
			foreach($users as &$user) {
				$query->reset();
				$query->from('Db_Record_Status')
					->where('userId = ?', $user['id'])
					->orderBy('date DESC')					
					->limit(1);
				$user['status'] = $query->fetchOne(array(), Doctrine::HYDRATE_ARRAY);				
			}
			$this->view->latestStatuses = $users;
		}
	}
	
	public function statusFormAction()
	{
		if ( Zend_Auth::getInstance()->hasIdentity() ) {
			$this->_statusForm();		
		}
		else {
			$this->_helper->viewRenderer->setNoRender();
		}
	}	
	
	private function _statuses($userId)
	{
		// Statuses of current user
        $selectionData = $this->config->getAction('user')->getSelection('statusSelection');
		$query = new Doctrine_Query();
		$query->parseDqlQuery($selectionData->getDql('Db_Record_Status'));
		$query->where('userId=?', $userId);
		$query->orderBy('date DESC');		
		
        $statuses = new Zend_Paginator(new SmartL_Zend_Paginator_Adapter_Doctrine($query));
        $statuses->setCurrentPageNumber($this->_getParam('page'));
        $statuses->setItemCountPerPage($this->config->action['user']->messagesPerPage->getValue());		        
		
		$this->view->statuses = $statuses;
	}
	
	private function _statusForm()
	{
		$form = SmartL_Zend_Form::getForm($this->config->getAction('user')->getForm('statusForm'), $this->translations['default']);
		$this->view->statusForm = $form;
		require 'Application/Config/Messages.php';
		$lang = current($this->application->getLanguages())->getLanguageId();
		$this->view->messages = $messages[$lang];
		
		if ( isset($_POST['message']) ) {
			$this->_handleStatusForm($form);
		}		
	}
	
	private function _handleStatusForm($form)
	{
		if ( $form->isValid($_POST) ) {
			$status = new Db_Record_Status();
			$status->message = $form->message->getValue();
			$status->userId = $this->application->getLoginInstance()->getUserId();
			$status->save();
			$this->_helper->redirector->setGotoRoute(array(), 'default', false);
		}	
	}
}